package com.basin.admin.controller;

import cn.dev33.satoken.filter.SaFilterAuthStrategy;
import cn.dev33.satoken.stp.StpUtil;
import com.basin.api.client.AuthClient;
import com.basin.common.core.response.Resp;
import com.basin.common.satoken.annotation.IgnoreGlobalAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author: Sam ZHONG | sammy1997@aliyun.com
 */
@Tag(name = "测试控制器")
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@IgnoreGlobalAuth
@Slf4j
public class TestController {
    private final AuthClient authClient;

    @Operation(summary = "打招呼")
    @GetMapping
//    @IgnoreGlobalAuth(authStrategy = TestAuthStrategy.class)
    public Resp<String> test1(@RequestParam(name = "msg") String msg) {
        boolean login = StpUtil.isLogin();
//        if (!login) {
//            StpUtil.login("test");
//        }
        return Resp.success("Hello1, " + msg + ", your login state is " + login);
    }

    @PutMapping
    public Resp<String> test3(@RequestBody Map<String, String> requestParams) {
        boolean login = StpUtil.isLogin();
        return Resp.success("Hello2, " + requestParams.get("msg") + ", your login state is " + login);
    }

    @PutMapping("/2/{msg}")
    public Resp<String> test2(@PathVariable(name = "msg") String msg) {
        boolean login = StpUtil.isLogin();
        authClient.getUriAuthConfig("/abcd");
        new Thread(() -> {
            try {
                Thread.sleep(2000);
                authClient.getWhiteUris();

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        log.info("======！！！！！！！");
        return Resp.success("Hello2, " + msg + ", your login state is " + login);
    }

    @PostMapping
    public void test4(@RequestBody Map<String, String> requestParams) {

    }


    @Slf4j
    public static class TestAuthStrategy implements SaFilterAuthStrategy {

        @Override
        public void run(Object obj) {
            log.info("自定义鉴权策略触发，入参={}", obj);
        }
    }
}
